<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
>
<head th:include="includes/head :: htmlhead(~{::title}, ~{})">
    <meta charset="utf-8">
    <title>web终端管理</title>
</head>
<body>
<div id="mainLayout" class="easyui-layout" data-options="fit: true">
    <div data-options="region:'west',title:'服务器列表'" style="width:80%;padding:0px;">
        <div style="padding:0px;height:calc(99% - 30px)">
            <table id='dg' style="width:100%;height:100%"></table>
        </div>
        <div id="pg" style="height:30px" class="easyui-pagination">
        </div>
    </div>
    <div data-options="region:'east',title:'服务器用户'" style="width:20%;padding:0px;">
        <div style="padding:0px;height:100%">
            <table id='dgServerUser' style="width:100%;height:100%"></table>
        </div>
    </div>

    <!-- 添加模块 -->
    <div id="adddlg" class="easyui-dialog" style="width:600px;height:230px;padding:10px 20px"
         closed="true" closable="false" modal="true" buttons="#dlg-buttons">
        <form id="addfm" method="post" novalidate>
            <input name="id" type="hidden"/>
            <table cellpadding="5">
                <tr>
                    <td>服务器名称:</td>
                    <td><input name="serverName" class="easyui-textbox" type="text" required="true" missingMessage="不能为空"/></td>
                    <td>服务器地址:</td>
                    <td><input name="serverHost" class="easyui-textbox" type="text" required="true" missingMessage="不能为空"/></td>
                </tr>
                <tr>
                    <td>服务器端口:</td>
                    <td><input name="serverPort" class="easyui-textbox" type="text" required="true" missingMessage="不能为空"/></td>
                </tr>
            </table>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#adddlg').dialog('close')">取消</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveAddFm()">保存</a>
    </div>

    <!-- 添加登录用户模块 -->
    <div id="addServerUserDlg" class="easyui-dialog" style="width:400px;height:200px;padding:10px 20px"
         closed="true" closable="false" modal="true" buttons="#addServerUserDlg-buttons">
        <form id="addServerUserFm" method="post" novalidate>
            <input name="id" type="hidden"/>
            <input id="addServerUserServerId" name="serverId" type="hidden"/>
            <table cellpadding="5">
                <tr>
                    <td>登录用户帐号:</td>
                    <td><input name="serverUser" class="easyui-textbox" type="text" required="true" missingMessage="不能为空"/></td>
                </tr>
                <tr>
                    <td>登录用户密码:</td>
                    <td><input name="serverPassword" class="easyui-passwordbox" type="text" required="true" missingMessage="不能为空"/></td>
                </tr>
            </table>
        </form>
    </div>
    <div id="addServerUserDlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel"
           onclick="javascript:$('#addServerUserDlg').dialog('close')">取消</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveAddServerUserFm()">保存</a>
    </div>

    <!-- 授权模块 -->
    <div id="signUserDlg" class="easyui-dialog" style="width:600px;height:270px;padding:0px 0px"
         closed="true" closable="true" modal="true">
        <div style="padding:0px;height:calc(100% - 30px)">
            <table id='dgUser' style="width:100%;height:100%"></table>
        </div>
        <input id="unsignedUserComboBox" name="userId" height="100%" data-options="panelHeight:'auto'">
        <a id="addUserBtn" href="#" class="easyui-linkbutton">添加登录用户</a>
    </div>

</div>

<script type="text/javascript">
    let ctxPath = '[[${#servletContext.contextPath}]]';
    $(function () {
        let toolbar = [{
            text: '添加',
            iconCls: 'icon-diy-add1',
            handler: function () {
                openAddDlg();
            }
        }, {
            text: '删除',
            iconCls: 'icon-diy-delete1',
            handler: function () {
                del();
            }
        }, {
            text: '修改',
            iconCls: 'icon-diy-edit1',
            handler: function () {
                openEditDlg();
            }
        }, '-', {
            text: '刷新',
            iconCls: 'icon-diy-refresh1',
            handler: function () {
                pagingList(1, COMMONPARAM_DEFAULTPAGESIZE);
            }
        }];
        $('#dg').datagrid({
            data: []
            , columns: [
                [
                    {field: 'serverName', title: '服务器名称', resizable: true}
                    , {field: 'serverHost', title: '服务器地址', resizable: true}
                    , {field: 'serverPort', title: '服务器端口', resizable: true}
                ]
            ]
            , onDblClickRow: function (field, row) {
                getServerUsersByServerId(row.id);
            }
            , autoRowHeight: true
            , rownumbers: true
            , singleSelect: true
            , toolbar: toolbar
        });

        $('#pg').pagination({
            pageList: COMMONPARAM_PAGELIST,
            pageSize: COMMONPARAM_DEFAULTPAGESIZE,
            total: 0,
            pageNumber: 1,
            beforePageText: '第',
            afterPageText: '页    共 {pages} 页',
            displayMsg: '当前显示 {from} - {to}条记录,共{total}条记录'
        });

        let toolbarDgServerUser = [
            {
                text: '添加',
                iconCls: 'icon-diy-add1',
                handler: function () {
                    openAddServerUserDlg();
                }
            }, {
                text: '删除',
                iconCls: 'icon-diy-delete1',
                handler: function () {
                    delServerUser();
                }
            },
            {
                text: '授权',
                iconCls: 'icon-diy-privilige1',
                handler: function () {
                    openSignUserDlg();
                }
            }];

        $('#dgServerUser').datagrid({
            data: []
            , columns: [
                [
                    {field: 'serverUser', title: '服务器用户', width: 200}
                ]
            ]
            , autoRowHeight: true
            , rownumbers: false
            , singleSelect: true
            , toolbar: toolbarDgServerUser
        });

        let toolbarDgUser = [{
            text: '删除',
            iconCls: 'icon-diy-delete1',
            handler: function () {
                delUser();
            }
        }];

        $('#dgUser').datagrid({
            data: []
            , columns: [
                [
                    {field: 'userCode', title: '登录帐号', width: 200}
                    , {field: 'userName', title: '用户名称', width: 200}
                ]
            ]
            , autoRowHeight: true
            , rownumbers: false
            , singleSelect: true
            , toolbar: toolbarDgUser
        });

        pagingList(1, COMMONPARAM_DEFAULTPAGESIZE);
    });

    function pagingList(pageNumber, pageSize) {
        let submitData = {};
        submitData['page'] = pageNumber;
        submitData['limit'] = pageSize;
        postJSON(ctxPath + '/mdmsSshserver/pagingList', submitData,
            function (data) {
                $('#dg').datagrid({
                    data: data['list']
                })
                $('#pg').pagination({
                    pageList: COMMONPARAM_PAGELIST,
                    pageSize: data['limit'],
                    total: data['total'],
                    pageNumber: data['page'],
                    beforePageText: '第',
                    afterPageText: '页    共 {pages} 页',
                    displayMsg: '当前显示 {from} - {to}条记录,共{total}条记录',
                    onSelectPage: function (pageNumber, pageSize) {
                        pagingList(pageNumber, pageSize);//选择第几页的时候，就刷新
                    },
                });
            }, function (result, resultdesc) {
                $.messager.show({
                    title: '异常',
                    msg: result + '-' + resultdesc,
                    showType: 'show'
                });
            }, function () {
                alert(3)
            }, function () {
                $.messager.progress(COMMONPARAM_PROCESS);
            }, function () {
                $.messager.progress('close');
            });
    }

    function openAddDlg() {
        COMMONPARAM_WEBOPERATION = 'add';
        $('#adddlg').dialog('open').dialog('setTitle', '添加服务器');
        $('#addfm').form('clear');
    }

    function openEditDlg() {
        COMMONPARAM_WEBOPERATION = 'edit';
        let row = $('#dg').datagrid('getSelected');
        if (row) {
            $('#adddlg').dialog('open').dialog('setTitle', '修改服务器');
            $('#addfm').form('load', row);
        } else {
            $.messager.show({
                title: '提示',
                msg: '请选择一条记录!',
                showType: 'show'
            });
        }
    }

    function saveAddFm() {
        if (!$('#addfm').form('validate')) {
            return;
        }

        let submitData = geneSubmitDataFromForm("#addfm", {});
        if (COMMONPARAM_WEBOPERATION == 'add') {
            delete submitData['id'];
        }
        postJSON(ctxPath + '/mdmsSshserver/' + COMMONPARAM_WEBOPERATION, submitData,
            function (data) {
                pagingList(1, COMMONPARAM_DEFAULTPAGESIZE);
                $('#adddlg').dialog('close');
            }, function (result, resultdesc) {
                $.messager.show({
                    title: '异常',
                    msg: result + '-' + resultdesc,
                    showType: 'show'
                });
            }, function () {
                alert(3)
            }, function () {
                $.messager.progress(COMMONPARAM_PROCESS);
            }, function () {
                $.messager.progress('close');
            });
    }

    function del() {
        COMMONPARAM_WEBOPERATION = 'del';
        let row = $('#dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('提示', '你确定要删除该条记录?', function (r) {
                if (r) {
                    let submitData = {};
                    submitData['id'] = row['id'];
                    postJSON(ctxPath + '/mdmsSshserver/' + COMMONPARAM_WEBOPERATION, submitData,
                        function (data) {
                            pagingList(1, COMMONPARAM_DEFAULTPAGESIZE);
                        }, function (result, resultdesc) {
                            $.messager.show({
                                title: '异常',
                                msg: result + '-' + resultdesc,
                                showType: 'show'
                            });
                        }, function () {
                            alert(3)
                        }, function () {
                            $.messager.progress(COMMONPARAM_PROCESS);
                        }, function () {
                            $.messager.progress('close');
                        });
                }
            });
        } else {
            $.messager.show({
                title: '提示',
                msg: '请选择一条记录!',
                showType: 'show'
            });
        }
    }

    function getServerUsersByServerId(serverId) {
        let submitData = {};
        submitData['serverId'] = serverId;
        postJSON(ctxPath + '/mdmsSshserver/getServerUsersByServerId', submitData,
            function (data) {
                $('#dgServerUser').datagrid({
                    data: data['list']
                });
            }, function (result, resultdesc) {
                $.messager.show({
                    title: '异常',
                    msg: result + '-' + resultdesc,
                    showType: 'show'
                });
            }, function () {
                alert(3)
            }, function () {
                $.messager.progress(COMMONPARAM_PROCESS);
            }, function () {
                $.messager.progress('close');
            });
    }

    function openAddServerUserDlg() {
        let rowSshServer = $('#dg').datagrid('getSelected');
        if (!rowSshServer) {
            $.messager.show({
                title: '提示',
                msg: '请选择一个服务器!',
                showType: 'show'
            });
            return;
        }


        COMMONPARAM_WEBOPERATION = 'add';
        $('#addServerUserDlg').dialog('open').dialog('setTitle', '添加登录用户');
        $('#addServerUserFm').form('clear');

        $('#addServerUserServerId').val(rowSshServer.id);
    }

    function saveAddServerUserFm() {
        if (!$('#addServerUserFm').form('validate')) {
            return;
        }

        let submitData = geneSubmitDataFromForm("#addServerUserFm", {});
        if (COMMONPARAM_WEBOPERATION == 'add') {
            delete submitData['id'];
        }
        postJSON(ctxPath + '/mdmsSshserver/' + COMMONPARAM_WEBOPERATION + 'ServerUser', submitData,
            function (data) {
                $.messager.show({
                    title: '提示',
                    msg: '操作成功',
                    showType: 'show'
                });
                getServerUsersByServerId(submitData['serverId']);
                $('#addServerUserDlg').dialog('close');
            }, function (result, resultdesc) {
                $.messager.show({
                    title: '异常',
                    msg: result + '-' + resultdesc,
                    showType: 'show'
                });
            }, function () {
                alert(3)
            }, function () {
                $.messager.progress(COMMONPARAM_PROCESS);
            }, function () {
                $.messager.progress('close');
            });

    }

    function delServerUser() {
        let row = $('#dgServerUser').datagrid('getSelected');
        if (row) {
            $.messager.confirm('提示', '你确定要删除该条记录?', function (r) {
                let submitData = {};
                submitData['id'] = row.id;
                postJSON(ctxPath + '/mdmsSshserver/delServerUser', submitData,
                    function (data) {
                        $.messager.show({
                            title: '提示',
                            msg: '删除服务器用户成功',
                            showType: 'show'
                        });
                        getServerUsersByServerId(row.serverId);
                    }, function (result, resultdesc) {
                        $.messager.show({
                            title: '异常',
                            msg: result + '-' + resultdesc,
                            showType: 'show'
                        });
                    }, function () {
                        alert(3)
                    }, function () {
                        $.messager.progress(COMMONPARAM_PROCESS);
                    }, function () {
                        $.messager.progress('close');
                    });
            });
        } else {
            $.messager.show({
                title: '提示',
                msg: '请选择一个服务器用户!',
                showType: 'show'
            });
        }
    }

    function openSignUserDlg() {
        let row = $('#dgServerUser').datagrid('getSelected');
        if (row) {
            let submitData = {};
            submitData['serverUserId'] = row.id;
            postJSON(ctxPath + '/mdmsSshserver/getUsersForSshServerSigned', submitData,
                function (data) {
                    $('#signUserDlg').dialog('open').dialog('setTitle', '授权');
                    $('#dgUser').datagrid({
                        data: data['signedList']
                    });
                    $('#unsignedUserComboBox').combobox({
                        data: data['unsignedList'],
                        valueField: 'id',
                        textField: 'userName'
                    });
                }, function (result, resultdesc) {
                    $.messager.show({
                        title: '异常',
                        msg: result + '-' + resultdesc,
                        showType: 'show'
                    });
                }, function () {
                    alert(3)
                }, function () {
                    $.messager.progress(COMMONPARAM_PROCESS);
                }, function () {
                    $.messager.progress('close');
                });
        } else {
            $.messager.show({
                title: '提示',
                msg: '请选择一个服务器用户!',
                showType: 'show'
            });
        }
    }

    $('#addUserBtn').bind('click', function () {
        let userId = $('#unsignedUserComboBox').combobox('getValue');
        if ('' == userId) {
            $.messager.show({
                title: '提示',
                msg: '请选择登录用户',
                showType: 'show'
            });
            return;
        }

        let row = $('#dgServerUser').datagrid('getSelected');
        if (row) {
            let submitData = {};
            submitData['serverId'] = row.serverId;
            submitData['userId'] = userId;
            submitData['serverUserId'] = row.id;
            postJSON(ctxPath + '/mdmsSshserver/signUser', submitData,
                function (data) {
                    $('#signUserDlg').dialog('close');
                    $.messager.show({
                        title: '提示',
                        msg: '授权成功',
                        showType: 'show'
                    });
                }, function (result, resultdesc) {
                    $.messager.show({
                        title: '异常',
                        msg: result + '-' + resultdesc,
                        showType: 'show'
                    });
                }, function () {
                    alert(3)
                }, function () {
                    $.messager.progress(COMMONPARAM_PROCESS);
                }, function () {
                    $.messager.progress('close');
                });
        } else {
            $.messager.show({
                title: '提示',
                msg: '请选择一个服务器用户!',
                showType: 'show'
            });
        }

    })
    ;

    function delUser() {
        let rowServerUser = $('#dgServerUser').datagrid('getSelected');
        if (!rowServerUser) {
            $.messager.show({
                title: '提示',
                msg: '请选择一个服务器用户!',
                showType: 'show'
            });
        }

        let row = $('#dgUser').datagrid('getSelected');
        if (row) {
            let submitData = {};
            submitData['serverUserId'] = rowServerUser.id;
            submitData['userId'] = row.id;
            postJSON(ctxPath + '/mdmsSshserver/delSignUser', submitData,
                function (data) {
                    $('#signUserDlg').dialog('close');
                    $.messager.show({
                        title: '提示',
                        msg: '删除登录用户成功',
                        showType: 'show'
                    });
                }, function (result, resultdesc) {
                    $.messager.show({
                        title: '异常',
                        msg: result + '-' + resultdesc,
                        showType: 'show'
                    });
                }, function () {
                    alert(3)
                }, function () {
                    $.messager.progress(COMMONPARAM_PROCESS);
                }, function () {
                    $.messager.progress('close');
                });
        } else {
            $.messager.show({
                title: '提示',
                msg: '请选择一个登录用户!',
                showType: 'show'
            });
        }
    }

</script>
</body>